package com.yunshang.shoppingmall.controller.adminLogin;


import com.yunshang.shoppingmall.common.MD5Util;
import com.yunshang.shoppingmall.entity.Admin;
import com.yunshang.shoppingmall.entity.Config;
import com.yunshang.shoppingmall.entity.PrivilegeURL;
import com.yunshang.shoppingmall.entity.UserRolePrivileges;
import com.yunshang.shoppingmall.service.AdminService;
import com.yunshang.shoppingmall.service.ConfigService;
import com.yunshang.shoppingmall.service.PrivilegeURLService;
import com.yunshang.shoppingmall.service.UserRolePrivilegesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 后台登录
 */
@Controller
public class AdminLoginController {
    @Autowired
    AdminService adminService;
    @Autowired
    ConfigService configService;
    @Autowired
    UserRolePrivilegesService userRolePrivilegesService;
    @Autowired
    PrivilegeURLService  privilegeURLService;

    @RequestMapping("/home")
    public String home(){
        return "/home";
    }

    //进入后台登录页面
    @RequestMapping("/sysLogin")
    public String inLoginHtml(){
        return "admin/adminLogin";
    }

    //后台登录进入后台首页
    @RequestMapping(value = "/sysAdminLogin",method = RequestMethod.POST)
    public String adminLogin(@RequestParam("username") String username,@RequestParam("password") String password, HttpServletRequest request, Map<String,Object> map , HttpSession session){
       boolean flag=false;
       List<PrivilegeURL> privilegeURLList=null;
       Map<String,Object> map1=new HashMap<>();
        if(username!=""&&password!=""){
            Admin admin=adminService.selectAdminByUserName(username);
            if(admin!=null){
                String pwdInDb=admin.getPwd();
                String pwd=password;
                try {
                   flag = MD5Util.validPassword(pwd, pwdInDb);//验证密码是否正确
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(username.equals(admin.getAdminName())&&flag){
                    //登入成功
                    List<UserRolePrivileges> userRolePrivilegesList =userRolePrivilegesService.selectUserRolePrivilegesByAdminId(admin.getAdminId());
                    session.setAttribute("userRolePrivilegesList",userRolePrivilegesList);
                    privilegeURLList= privilegeURLService.selectPriURLList();
                    String URLkey=null;
                    int URLVal=0;
                    for (int i = 0; i <privilegeURLList.size() ; i++) {
                        URLkey=privilegeURLList.get(i).getResourceUrl();
                        URLVal=privilegeURLList.get(i).getRoleId();
                        map1.put(URLkey,URLVal);
                    }
                    session.setAttribute("map",map1);

                    session.setAttribute("privilegeURLList", privilegeURLList);
                    session.setAttribute("admin",admin);
                    session.setAttribute("loginAdmin",username);
                    session.setAttribute("adminId",admin.getAdminId());
                    return "redirect:inSysIndex";
                }else{
                    //登录失败
                    map.put("msg","用户名或密码错误！");
                    return "admin/adminLogin";
                }

            }else {
                map.put("msg", "用户名或密码错误！");
                return "admin/adminLogin";
            }
        }
        map.put("msg", "用户名或密码为空！");
        return "admin/adminLogin";
    }

    //进入后台首页页面
    @RequestMapping("/inSysIndex")
    public ModelAndView inSysIndex(){
        Config config= configService.selectByPrimaryKey(1);

        ModelAndView modelAndView=new ModelAndView("sysIndex");

        modelAndView.addObject("WebConfig",config);
        return modelAndView;
    }


    //退出

}
